package com.uroad.jilinpda.widget;

import android.os.Handler;

/**
 * Created by admin(wlw) on 2018/7/9.
 * 倒计时控制器 建议在acvitity 结束时调用cancel()方法防止内存泄漏
 */
public class TimeCountController {
    /**倒计时当前状态*/
    private static int currStatus;
    /**倒计时准备状态*/
    private static int STATUS_PREPARE = 0001;
    /**倒计时运行状态*/
    private static int STATUS_RUNNING = 0002;
    /**倒计时结束状态*/
    private static int STATUS_FINISH = 0003;
    /**倒计时取消状态*/
    private static int STATUS_CANCEL = 0004;

    /**是否进行倒计时*/
    private boolean isTickEnable = true;
    /**倒计时总时间*/
    private long timeSecondCache = 60;
    /**倒计时总时间*/
    private long timeSecond = 60;
    /**倒计时间隔时间*/
    private long countSecond = 1;
    /**是否需要返回0秒 默认不返回*/
    private boolean isBackZero = false;
    /**状态监听*/
    private OnTimeCountListener onTimeCountListener;
    /**延时执行的handler*/
    private Handler handler = new Handler();

    /**
     * 倒计时类  建议在acvitity 结束时调用cancel()方法防止内存泄漏
     * @param timeSecond 倒计时总时间 单位毫秒
     * @param countSecond 倒计时间隔时间 单位毫秒
     * @param onTimeCountListener 倒计时监听
     */
    public TimeCountController(long timeSecond, long countSecond, OnTimeCountListener onTimeCountListener) {
        init(timeSecond,countSecond,onTimeCountListener);
    }

    private void init(long timeSecond, long countSecond, OnTimeCountListener onTimeCountListener) {
        this.timeSecond = timeSecond;
        this.timeSecondCache = this.timeSecond;
        this.countSecond = countSecond;
        this.onTimeCountListener = onTimeCountListener;
        currStatus = STATUS_PREPARE;
    }

    /**
     * 倒计时
     */
    private synchronized void startTimeCount() {
        if (isTickEnable) {
            //如果需要返回0 则将此处改为>=0即可
            if (timeSecond > 0) {
                onTick();
            } else if (timeSecond <= 0) {
                if (isBackZero && timeSecond < -countSecond) {
                    onTick();
                } else {
                    onFinish();
                }
            }
        } else {
            cancel();
        }
    }

    private void onTick(){
        isTickEnable = true;
        if (onTimeCountListener != null) {
            onTimeCountListener.onTick(timeSecond);
        }
        timeSecond -= countSecond;
        handler.postDelayed(timeRunable, countSecond);
    }

    private void onFinish() {
        currStatus = STATUS_FINISH;
        timeSecond = timeSecondCache;
        if (onTimeCountListener != null) {
            onTimeCountListener.onFinish();
        }
    }

    private Runnable timeRunable = new Runnable() {
        @Override
        public void run() {
            startTimeCount();
        }
    };

    /**
     * 是否需要返回0秒 默认不返回
     * @param isBackZero true 返回 false 不返回
     */
    public void isBackZero(boolean isBackZero) {
        this.isBackZero = isBackZero;
    }

    /**
     * 开始
     */
    public synchronized void start(){
        if (!isTickEnable) {
            isTickEnable = true;
        }
        if (currStatus != STATUS_RUNNING) {
            currStatus = STATUS_RUNNING;
            startTimeCount();
        }
    }
    /**
     * 取消
     */
    public synchronized void cancel(){
        if (currStatus != STATUS_CANCEL) {
            currStatus = STATUS_CANCEL;
            isTickEnable = false;
            handler.removeCallbacks(timeRunable);
            if (onTimeCountListener != null) {
                onTimeCountListener.onCancel();
            }
        }
    }

    /**
     * 倒计时状态监听
     */
    public interface OnTimeCountListener{
        /**
         * 倒计时操作
         * @param time 倒计时 返回总时间 - 间隔时间
         */
        void onTick(long time);
        /**倒计时结束*/
        void onFinish();
        /**倒计时取消*/
        void onCancel();
    }
}
